<?php

namespace app\models;

use yii;
use yii\db\Query;

class Purchase extends BaseModel
{

    public function __construct()
    {
        $this->table = yii::$app->db->tablePrefix . 'purchase';
    }

    public function one($where = [])
    {
        $db = new Query();
        $db->select('*')->from($this->table);
        if (!empty($where['id'])) {
            $db->andWhere(['id' => $where['id']]);
        }
        if (isset($where['purchase_num'])) {
            $db->andWhere(['purchase_num' => $where['purchase_num']]);
        }

        return $db->one();
    }

    public function all($where = [], $offset = false, $limit = false, $order = false)
    {
        $db = new Query();
        $db->select('*')->from($this->table);
        if (!empty($where['invoice'])) {
            if (is_array($where['invoice'])) {
                $db->andWhere(['in', 'invoice', $where['invoice']]);
            } else {
                $db->andWhere(['invoice' => $where['invoice']]);
            }
        }
        if (isset($where['purchase_num'])) {
            if (is_array($where['purchase_num'])) {
                $db->andWhere(['in', 'purchase_num', $where['purchase_num']]);
            } else {
                $db->andWhere(['purchase_num' => $where['purchase_num']]);
            }
        }
        if ($offset) {
            $db->offset($offset);
        }

        if ($limit) {
            $db->limit($limit);
        }

        if ($order) {
            $db->orderBy($order);
        }

        return $db->all();
    }

    /**采购列表
     * @param $p
     * @param $s
     * @param array $where
     * @return array
     */
    public function get_purchase_list($p, $s, $where = [])
    {
        $db = new Query();
        $db->select('*')->from($this->table);
        if (!empty($where['s_num'])) {
            $db->andWhere(['s_num' => $where['s_num']]);
        }
        if (!empty($where['invoice'])) {
            $db->andWhere(['invoice' => $where['invoice']]);
        }
        if (!empty($where['last_modify'])) {
            $db->andWhere(['last_modify' => $where['last_modify']]);
        }
        if (!empty($where['purchase_num'])) {
            $db->andWhere(['purchase_num' => $where['purchase_num']]);
        }
        if (!empty($where['pay_status'])) {
            $db->andWhere(['pay_status' => $where['pay_status']]);
        }
        if (!empty($where['job_number'])) {
            if (is_array($where['job_number'])) {
                $db->andWhere(['in', 'job_number', $where['job_number']]);
            } else {
                $db->andWhere(['job_number' => $where['job_number']]);
            }
        }
        if (!empty($where['product_status'])) {
            $db->andWhere(['product_status' => $where['product_status']]);
        }
        if (!empty($where['product_status_time'])) {
            $db->andWhere(['product_status_time' => $where['product_status_time']]);
        }
        return $db->orderBy(['id' => SORT_DESC])->offset(($p - 1) * $s)->limit($s)->all();
    }

    /**采购size
     * @param array $where
     * @return int
     */
    public function get_purchase_size($where = [])
    {
        $db = new Query();
        $db->select('count(*) as num')->from($this->table);
        if (!empty($where['s_num'])) {
            $db->andWhere(['s_num' => $where['s_num']]);
        }
        if (!empty($where['invoice'])) {
            $db->andWhere(['invoice' => $where['invoice']]);
        }
        if (!empty($where['last_modify'])) {
            $db->andWhere(['last_modify' => $where['last_modify']]);
        }
        if (!empty($where['purchase_num'])) {
            $db->andWhere(['purchase_num' => $where['purchase_num']]);
        }
        if (!empty($where['pay_status'])) {
            $db->andWhere(['pay_status' => $where['pay_status']]);
        }
        if (!empty($where['job_number'])) {
            if (is_array($where['job_number'])) {
                $db->andWhere(['in', 'job_number', $where['job_number']]);
            } else {
                $db->andWhere(['job_number' => $where['job_number']]);
            }
        }
        if (!empty($where['product_status'])) {
            $db->andWhere(['product_status' => $where['product_status']]);
        }
        if (!empty($where['product_status_time'])) {
            $db->andWhere(['product_status_time' => $where['product_status_time']]);
        }
        $re = $db->one();
        return $re ? $re['num'] : 0;
    }

    /**更新记录
     * @param $data
     * @param $id
     * @return int
     */
    public function updateByParam($data, $id)
    {
        try {
            return yii::$app->db->createCommand()
                ->update($this->table, $data, ['id' => $id])
                ->execute();
        } catch (\Exception $e) {
            yii::error($e->getMessage());
            return false;
        }

    }

    public function check_purchase_num($num)
    {
        return (new Query())->select('*')
            ->from($this->table)
            ->where(['purchase_num' => $num])
            ->limit(1)
            ->one();
    }

    public function insert_purchase($data)
    {
        try {
            return yii::$app->db->createCommand()
                ->insert($this->table, $data)
                ->execute();
        } catch (\Exception $e) {
            yii::error($e->getMessage());
            return false;
        }

    }

    public function get_purchase_by_invoice($invoice)
    {
        $db = new Query();
        return $db->select('*,b.name as zh_name')
            ->from(yii::$app->db->tablePrefix . 'purchase as a')
            ->join('LEFT JOIN', yii::$app->db->tablePrefix . 'staff as b', 'a.job_number=b.job_number')
            ->where(['invoice' => $invoice])
            ->one();
    }

    public function edit_purchase_by_invoice($invoice, $data)
    {
        return yii::$app->db->createCommand()
            ->update($this->table, $data, ['invoice' => $invoice])
            ->execute();
    }

    //生成一条采购编号
    public function generate_purchase_num()
    {
        $db   = new Query();
        $info = $db->select('count(purchase_num) as num')
            ->from(yii::$app->db->tablePrefix . 'purchase')
            ->where(['create_at' => date('Y-m-d')])
            ->one();
        if ($info) {

            $count = $info['num'] + 1;
            do {
                $num = 'CG' . date('ymd') . str_pad($count, 3, 0, STR_PAD_LEFT);
                $db  = new Query();
                $re  = $db->select('*')->from($this->table)->where(['purchase_num' => $num])->one();
                if (!$re) {
                    return $num;
                }
                $count++;

            } while ($re);
        }
    }

    //
    public function get_purchase_by_id($id)
    {
        $db   = new Query();
        $info = $db->select('a.*,b.name as zh_name')
            ->from(yii::$app->db->tablePrefix . 'purchase as a')
            ->join('LEFT JOIN', yii::$app->db->tablePrefix . 'staff as b', 'a.job_number=b.job_number')
            ->where(['a.id' => $id])
            ->one();

        return $info;
    }

    /**
     *计算采购合同总支出
     *@return num
     */
    public function sumTotalPay($where)
    {
        $db = new Query();
        $db->select('sum(total_fee) as num')->from($this->table);
        if (!empty($where['job_number'])) {
            if (is_array($where['job_number'])) {
                $db->andWhere(['in', 'job_number', $where['job_number']]);
            } else {
                $db->andWhere(['job_number' => $where['job_number']]);
            }
        };
        $db->andWhere(['>=','create_at','2018-01-01']);
        $re = $db->one();
        return $re ? $re['num'] : 0;
    }

}
